package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private DestinationMapper destinationMapper;
    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo );
        travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> listVeiwnumTop3(Long destId) {
        return travelMapper.listVeiwnumTop3(destId);
    }

    @Override
    public TravelContent getContent(Long id) {
        return travelMapper.getContent(id);
    }

    @Override
    public AjaxResult saveOrUpdate(Travel travel) {
        AjaxResult result = new AjaxResult();
        //生成摘要
        travel.setSummary("人工智能生成...............");
        Date date = new Date();
        //最后更新时间
        travel.setLastUpdateTime(date);
        if (travel.getId() == null){//新增
            //设置创建时间
            travel.setCreateTime(date);
            //设置发布时间
            travel.setReleaseTime(date);
            travelMapper.insert(travel);
            //保存游记文章
            TravelContent content = travel.getTravelContent();
            content.setId(travel.getId());
            travelMapper.insertContent(content);
        }else{//更新
            travelMapper.updateByPrimaryKey(travel);
            //保存游记文章
            TravelContent content = travel.getTravelContent();
            content.setId(travel.getId());
            travelMapper.updateContent(content);
        }
        result.setData(travel.getId());
        return result;
    }

    @Override
    public List<Travel> selectByUserId(Long userId) {
        List<Travel> travels = travelMapper.selectByUserId(userId);
        for (Travel travel : travels) {
            TravelContent content = travelContentMapper.selectById(travel.getId());
            Destination destination = destinationMapper.selectByPrimaryKey(travel.getDest().getId());
            Destination parent = destination.getParent();
            travel.setParent(parent);
            travel.setTravelContent(content);
            System.out.println(travel);
        }
        return travels;
    }

    public Long selectByTravel(Long id) {
        return travelMapper.selectByTravelId(id);

    }

    public List<Travel> listAll() {
        return travelMapper.selectAll();
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }


    @Override
    public void updateState(Travel travel) {
        travelMapper.updateState(travel);
    }

    @Override
    public Travel selectById(Long travelId) {
        return travelMapper.selectByPrimaryKey(travelId);
    }
}
